package com.work.daily.stream;

import com.work.daily.stream.data.Person;

import java.util.List;
import java.util.Optional;

/**
 * Stream 的归约(reduce)
 *
 * @author Shengkang.Mo
 * @create 2020-11-27 15:24
 */
public class Stream_6_2 {
    public static void main(String[] args) {
        //求所有员工的工资之和和最高工资。
        List<Person> personList = Person.initData();

        //求工资之和方式1
        Optional<Integer> sumSalary = personList.stream().map(Person::getSalary).reduce(Integer::sum);

        //求工资之和方式2
        Integer sumSalary2 = personList.stream().reduce(0, (sum, p) -> sum += p.getSalary(),
                (sum1, sum2) -> sum1 + sum2);

        //求工资之和方式3
        Integer sumSalary3 = personList.stream().reduce(0, (sum, p) -> sum += p.getSalary(),
                Integer::sum);

        System.out.println("工资之和：" + sumSalary.get() + ";" + sumSalary2 + ";" + sumSalary3);

        //求最高工资方式1
        Integer maxSalary1 = personList.stream().reduce(0, (max, p) -> max > p.getSalary() ? max : p.getSalary(),
                Integer::max);

        //求最高工资方式2
        Integer maxSalary2 = personList.stream().reduce(0, (max, p) -> max > p.getSalary() ? max : p.getSalary(),
                (max1, max2) -> max1 > max2 ? max1 : max2);

        System.out.println("最高工资：" + maxSalary1 + ";" + maxSalary2);
    }
}
